<?php
/**
 * Created by PhpStorm.
 * User: ADMIN
 * Date: 2021/5/18
 * Time: 17:48
 */
namespace app\adminapi\controller;

use app\common\controller\AdminApi;
use think\captcha\Captcha;

/**
 * Class Index
 * @package app\adminapi\controller
 */
class Index extends AdminApi
{
    protected $noNeedLogin = ['captcha'];
    protected $noNeedRight = ['*'];

    /**
     * 全局miss路由
     */
    public function miss()
    {
        $this->error('非法访问', null, '404');
    }

    public function captcha()
    {
        $captcha = new Captcha();
        // 设置验证码字符为纯数字
        $captcha->codeSet = '0123456789';
        $captcha->length = 4;
        return $captcha->entry();
    }

    /**
     * 首页
     */
    public function index() {
        $data = [];
        // 用户总量
        $data['userCount'] = db('user')->count();

        // 搜索总数
        $data['searchCount'] = db('search_record')->count();

        // 变更总数
        $data['changeCount'] = 0;

        // 收入总额
        $data['incomeCount'] = db('order')
            ->where("status='3'")
            ->sum('goods_price');

        // 近一月数据每天搜索次数 变更次数
        $monthArr = getEmptyArr(date('Y-m-d',strtotime('last month')), date('Y-m-d'), 'day');
        $monthObj = new \stdClass;
        sort($monthArr);
        foreach($monthArr as $v) {
            $arr = [];
            // 搜索次数
            $arr[] = db('search_record')
                ->where('FROM_UNIXTIME(createtime, "%Y-%m-%d")="'.$v.'"')
                ->count();
            $arr[] = mt_rand(1,10);
            $monthObj->$v = $arr;
        }
        $data['monthObj'] = $monthObj;

        $this->success('查询成功', $data);
    }
}